﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;

namespace OMSService
{
    [DataContract]
    public class UserModel
    {
        int id;
        string login;
        string firstName;
        string lastName;
        string email;
        string password;
        string region;
        string role;
        bool isLocked;
        private const int saltLength = 20;

        #region Constructors

        public UserModel() { }
        
        public UserModel(string login, string firstName, string lastName,
            string email, string password, string region, string role) 
        {
            Login = login;
            FirstName = firstName;
            LastName = lastName;
            Email = email;
            Password = password;
            Region = region;
            Role = role;
        }

        public UserModel(User user)
        {
            Id = user.Id;
            Login = user.Login;
            FirstName = user.FirstName;
            LastName = user.LastName;
            Email = user.Email;
            Region = user.Region;
            Role = user.Role.UserRole;
        }

        #endregion

        #region Password

        public static string GenerateSalt()
        {
            byte[] randomArray = new byte[saltLength];
            string salt;
            //Create random salt and convert to string
            Random rnd = new Random();
            rnd.NextBytes(randomArray);
            salt = Convert.ToBase64String(randomArray);
            return salt;
        }

        public static string MakeHash(string pass, string salt)
        {
            UnicodeEncoding enc = new UnicodeEncoding();
            string text = pass + salt;
            byte[] hashValue;
            byte[] message = enc.GetBytes(text);
            SHA1Managed hashString = new SHA1Managed();
            string hex = "";
            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}",x);
            }
            return hex;
        }

        #endregion

        #region Setters
        [DataMember]
        public int Id
        {
            get { return id; }
            private set { id = value; }
        }

        [DataMember]
        public string Login
        {
            get { return login; }
            set { login = value; }
        }

        [DataMember]
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        [DataMember]
        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        [DataMember]
        public string Email
        {
            get { return email; }
            set { email = value; }
        }

        [DataMember]
        public string Password
        {
            get { return password; }
            set { password = value; } 
        }

        [DataMember]
        public string Region
        {
            get { return region; }
            set { region = value; }
        }

        [DataMember]
        public string Role
        {
            get { return role; }
            set { role = value; }
        }

        [DataMember]
        public bool IsLocked
        {
            get { return isLocked; }
            set { isLocked = value; }
        }
#endregion
    }
}